package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.Skzh;
import com.ruoyi.system.domain.Slmx;
import com.ruoyi.system.domain.Tidetails;
import com.ruoyi.system.domain.bo.SlmxBo;
import com.ruoyi.system.domain.bo.TiBo;
import com.ruoyi.system.domain.vo.SlmxVo;
import com.ruoyi.system.mapper.TiMapper;
    import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISkzhService;
import com.ruoyi.system.service.ISlmxService;
import com.ruoyi.system.service.ITidetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TiMapper;
import com.ruoyi.system.domain.Ti;
import com.ruoyi.system.service.ITiService;
import org.springframework.transaction.annotation.Transactional;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@Service
public class TiServiceImpl extends ServiceImpl<TiMapper, Ti> implements ITiService
{
    @Autowired
    private TiMapper tiMapper;
    @Autowired
    private ISlmxService slmxService;
    @Autowired
    private ITidetailsService tidetailsService;
    @Autowired
    private ISkzhService skzhService;

    /**
     * 查询【请填写功能名称】
     *
     * @param tiId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TiBo selectTiByTiId(Long tiId)
    {
        return tiMapper.selectTiByTiId(tiId);
    }

/**
 * 查询【请填写功能名称】列表
 *
 * @param ti 【请填写功能名称】
 * @return 【请填写功能名称】
 */

@Override
public List<TiBo> selectTiList(Ti ti)
{
    return tiMapper.selectTiList(ti);
}

    @Override
    public BigDecimal withdrawableBalance(Long userId)
    {
        return tiMapper.withdrawableBalance(userId);
    }

    @Override
    public BigDecimal withdrawnTotal(Long userId)
    {
        return tiMapper.withdrawnTotal(userId);
    }



    /**
     * 新增【请填写功能名称】
     *
     * @param ti 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTi(Ti ti, Skzh skzh)
    {
           ti.setCreateTime(DateUtils.getNowDate());
           SlmxVo slmxVo=new SlmxVo();
           slmxVo.setTlId(ti.getUserId());
           slmxVo.setWithdrawalStatus("1");
           List<SlmxBo> slmxBos = slmxService.selectSlmxList(slmxVo);
           if(slmxBos==null || slmxBos.size()==0){
               throw new RuntimeException("暂无可提现人员");
           }
         int i = tiMapper.insertTi(ti);
        skzhService.updateSkzh(skzh);

        List<Tidetails> tidetailsList=new ArrayList<>();
        List<Slmx> slmxList=new ArrayList<>();
        Tidetails tidetails=null;
        Slmx slmx=null;
        for (SlmxBo s:
        slmxBos) {
            tidetails =new Tidetails();
            slmx=new Slmx();
            tidetails.setTiId(ti.getTiId());
            tidetails.setSlmxId(s.getSlmxId());

            slmx.setSlmxId(s.getSlmxId());
            slmx.setWithdrawalStatus("2");
            tidetailsList.add(tidetails);
            slmxList.add(slmx);
        }

        tidetailsService.saveBatch(tidetailsList);
        slmxService.updateBatchById(slmxList);
        return i;
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param ti 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTi(Ti ti)
    {
        int i = tiMapper.updateTi(ti);
        Skzh skzh=new Skzh();
        skzh.setSkzhId(ti.getSkzhId());
        skzh.setWithdrawalStatus("1");
        skzhService.updateSkzh(skzh);
        LambdaQueryWrapper<Tidetails> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Tidetails::getSlmxId);
        lambdaQueryWrapper.eq(Tidetails::getTiId,ti.getTiId());
        List<Tidetails> list = tidetailsService.list(lambdaQueryWrapper);
        String state=null;
        List<Slmx> slmxList=new ArrayList<>();
        if(ti.getStatus().equals("2")){
            state="3";
        }else {
            state="1";
        }
        Slmx slmx=null;
        for (Tidetails s:list){
            slmx=new Slmx();
            slmx.setSlmxId(s.getSlmxId());
            slmx.setWithdrawalStatus(state);
            slmxList.add(slmx);
        }
        slmxService.updateBatchById(slmxList);
        return i;
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param tiIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTiByTiIds(Long[] tiIds)
    {
        return tiMapper.deleteTiByTiIds(tiIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param tiId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTiByTiId(Long tiId)
    {
        return tiMapper.deleteTiByTiId(tiId);
    }
}
